package Upload;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import Log.LogManager;

public class FileUpload extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public FileUpload() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			DiskFileItemFactory fif=new DiskFileItemFactory();
			fif.setSizeThreshold(1024*1024);
			ServletFileUpload sfu=new ServletFileUpload(fif);
			sfu.setSizeMax(1024*1024*1024);
			List items=null;
			try {
				items=sfu.parseRequest(request);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Iterator iter=items.iterator();
			String filename="";
			String filesize="";
			String filetype="";
			String caption="";
			FileItem fi=null;
			while (iter.hasNext()) {
			    FileItem item = (FileItem) iter.next();
			    if (item.isFormField()) {
			        String name=item.getFieldName();
			        if(name.equals("filename")==true){
			        	filename=item.getString("UTF-8");
			        }else if(name.equals("filesize")==true){
			        	filesize=item.getString("UTF-8");
			        }else if(name.equals("filetype")==true){
			        	filetype=item.getString("UTF-8");
			        }else if(name.equals("caption")==true){
			        	caption=item.getString("UTF-8");
			        }
			    } else {
			    	fi=item;
			    }
			}
			ServletContext application=getServletContext();
			String path=(String) application.getAttribute("datapath")+"uploadpath"+application.getAttribute("systempi")+filename;
			File f1=new File(path);
			try {
				fi.write(f1);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(path);
			LogManager.log("一个文件上传请求已经被受理！文件存储于："+path);
			out.flush();
			out.close();
		} catch (Exception e) {
			LogManager.err(e.toString());
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
